<template>
	<view class="search">
		<musichead title="搜索" :icon="true" :iconBlack="true"></musichead>
		<view class="container">
			<scroll-view scroll-y="true">
				<view class="search-search">
					<text class="iconfont iconsearch"></text>
					<input type="text" placeholder="搜索歌曲" v-model="searchWordData" @confirm="handleToSearch" @input="handleToSuggest" />
					<text v-show="searchType == 2" @tap="handleToClose" class="iconfont iconguanbi"></text>
				</view>
				<block v-if="searchType == 1">
					<view class="search-history">
						<view class="search-history-head">
							<text>历史记录</text>
							<text class="iconfont iconlajitong" @tap="handleToClear"></text>
						</view>
						<view class="search-history-list">
							<view v-for="(item,index) in historyListData.data" :key="index" @tap="handleToWord(item)">{{ item }}</view>
						</view>
					</view>
					<view class="search-hot">
						<view class="search-hot-title">热搜榜</view>
						<!-- <view class="search-hot-item">
							<view class="search-hot-top">1</view>
							<view class="search-hot-word">
								<view>
									少年 <image src="../../static/dujia.png" mode="aspectFit"></image>
								</view>
								<view>"少年"这个词实在是太美好了</view>
							</view>
							<text class="search-hot-count">2968644</text>
						</view> -->
						<view class="search-hot-item" v-for="(item,index) in searchHotData.data" :key="index" @tap="handleToWord(item.searchWord)">
							<view class="search-hot-top">{{ index + 1 }}</view>
							<view class="search-hot-word">
								<view>
									{{ item.searchWord }} <image :src="item.iconType ? item.iconUrl : ''" mode="aspectFit"></image>
								</view>
								<view>{{ item.content }}</view>
							</view>
							<text class="search-hot-count">{{ item.score }}</text>
						</view>
					</view>
				</block>
				<block v-else-if="searchType == 2">
					<view class="search-result">
						<!-- <view class="search-result-item">
							<view class="search-result-word">
								<view>少年</view>
								<view>
									<image src="../../static/dujia.png" mode=""></image>
									<image src="../../static/dujia.png" mode=""></image>
									许巍 - 爱如少年
								</view>
							</view>
							<text class="iconfont iconbofang"></text>
						</view> -->
						<view class="search-result-item" v-for="(item,index) in searchListData.data" :key="index" @tap="handleToDetail(item.id)">
							<view class="search-result-word">
								<view>{{ item.name }}</view>
								<view>{{ item.al.name }} - {{ item.ar[0].name }}</view>
							</view>
							<text class="iconfont iconbofang"></text>
						</view>
					</view>
				</block>
				<block v-else-if="searchType == 3">
					<view class="search-suggest">
						<view class="search-suggest-title">搜索"{{ searchWordData }}"</view>
						<!-- <view class="search-suggest-item">
							<text class="iconfont iconsearch"></text>
							少年抖音
						</view> -->
						<view class="search-suggest-item" v-for="(item,index) in suggestList.data" :key="index" @tap="handleToWord(item.keyword)">
							<text class="iconfont iconsearch"></text>
							{{ item.keyword }}
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { searchHot , searchWord , searchSuggest } from '../../common/api.js'
	import {ref,reactive} from 'vue'
	import {onLoad} from '@dcloudio/uni-app'
	let searchHotData=reactive({
		data:[]
	})
	let searchWordData=ref('')
	let historyListData=reactive({
		data:[]
	})
	let searchType=ref(1)
	let searchListData=reactive({
		data:[]
	})
	let suggestList=reactive({
		data:[]
	})
	onLoad(()=>{
		searchHot().then((res)=>{
			console.log(res);	
			if(res.data.code == '200'){
				searchHotData.data = res.data.data;
			}
		});
		uni.getStorage({
		    key: 'searchHistory',
		    success:(res)=>{
		        historyListData.data = res.data;
		    }
		});
	})
	function handleToSearch(){
		historyListData.data.unshift(searchWordData.value);
		historyListData.data = [...new Set(historyListData.data)];
		if(historyListData.data.length > 10){
			historyListData.data.length = 10;
		}
		uni.setStorage({
			key: 'searchHistory',
			data: historyListData.data
		});
		getSearchList(searchWordData.value);
	}
	function handleToClear(){
		uni.removeStorage({
			key:'searchHistory',
			success:()=>{
				historyListData.data = [];
			}
		});
	}
	function getSearchList(word){
		searchWord(word).then((res)=>{
			if(res.data.code == '200'){
				searchListData.data = res.data.result.songs;
				searchType.value = 2;
			}
		});
	}
	function handleToClose(){
		searchWordData.value = '';
		searchType.value = 1;
	}
	function handleToSuggest(ev){
		let value = ev.detail.value;
		if(!value){
			searchType.value = 1;
			return;
		}
		searchSuggest(value).then((res)=>{
			if(res.data.code == '200'){
				suggestList.data = res.data.result.allMatch;
				searchType.value = 3;
			}
		});
	}
	function handleToWord(word){
		searchWordData.value = word;
		handleToSearch();
	}
	function handleToDetail(songId){
		uni.navigateTo({
			url: '/pages/detail/detail?songId='+songId
		});
	}
</script>

<style lang="scss" scoped>
	@import '../../common/iconfont.css';
	.search-search{ display: flex; background:#f7f7f7; height:73rpx; margin:28rpx 30rpx 30rpx 30rpx; border-radius: 50rpx; align-items: center;}
	.search-search text{ margin:0 27rpx;} 
	.search-search input{ font-size:26rpx; flex:1;}
	
	.search-history{ margin:0 30rpx; font-size:26rpx;}
	.search-history-head{ display: flex; justify-content: space-between;}
	.search-history-list{ display: flex; margin-top:36rpx; flex-wrap: wrap;}
	.search-history-list view{ padding:20rpx 40rpx; background:#f7f7f7; border-radius: 50rpx; margin-right:30rpx; margin-bottom: 20rpx;}
	
	.search-hot{ margin:30rpx 30rpx; font-size:26rpx; color:#bebebe;}
	.search-hot-title{}
	.search-hot-item{ display: flex; align-items: center; margin-top: 40rpx;}
	.search-hot-top{ width:60rpx; color:#fb2221; font-size:34rpx;}
	.search-hot-word{ flex:1;}
	.search-hot-word view:nth-child(1){ font-size:36rpx; color:black;}
	.search-hot-word image{ width:48rpx; height:22rpx;}
	.search-hot-count{}
	
	.search-result{ border-top: 2rpx #e5e5e5 solid; padding:30rpx;}
	.search-result-item{ display: flex; align-items: center; border-bottom: 2rpx #e5e5e5 solid; padding-bottom:30rpx; margin-bottom: 30rpx;}
	.search-result-item text{ font-size:50rpx;}
	.search-result-word{ flex:1;}
	.search-result-word view:nth-child(1){ font-size:28rpx; color:#3e6694;}
	.search-result-word view:nth-child(2){ font-size:26rpx;}
	
	.search-suggest{ border-top: 2rpx #e5e5e5 solid; padding:30rpx; font-size:26rpx; }
	.search-suggest-title{ color:#537caa; margin-bottom: 40rpx;}
	.search-suggest-item{ color:#666666; margin-bottom: 70rpx;}
	.search-suggest-item text{ color:#c2c2c2; font-size:26rpx; margin-right:26rpx;}
</style>
